﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace PhysicsSimulator.ThrowOblique
{
    public partial class EditThrowObliquePage : PhoneApplicationPage
    {
        public EditThrowObliquePage()
        {
            InitializeComponent();
            _alpha.Text = ThrowObliqueSettings.Angle.ToString();
            _v0.Text = ThrowObliqueSettings.Velocity.ToString();
            _g.Text = ThrowObliqueSettings.Gravity.ToString();
            _h_fist.Text = ThrowObliqueSettings.HeightFirst.ToString();

            v0_slider.Value = (ThrowObliqueSettings.Velocity - 5) / 45 * 10;
            alpha_slider.Value = (ThrowObliqueSettings.Angle) / 90 * 10;
            h_slider.Value = (ThrowObliqueSettings.HeightFirst) / 50 * 10;
            g_slider.Value = (ThrowObliqueSettings.Gravity - 5);
        }

        private void ApplyClick(object sender, System.EventArgs e)
        {
            if (float.Parse(_v0.Text) < 5 || float.Parse(_v0.Text) > 50)
            {
                MessageBox.Show("Vận tốc nằm trong khoảng 1-50m/s");
            }
            else
            {
                if (float.Parse(_alpha.Text) < 0 || float.Parse(_alpha.Text) > 90)
                {
                    MessageBox.Show("Góc hợp với mặt đất Alpha nằm trong khoảng 0-90°");
                }
                else
                {
                    if (float.Parse(_h_fist.Text) < 0 || float.Parse(_h_fist.Text) > 50)
                    {
                        MessageBox.Show("Độ cao ban đầu h nằm trong khoảng 0-30m");
                    }
                    else
                    {
                        if (float.Parse(_g.Text) < 5 || float.Parse(_g.Text) > 15)
                        {
                            MessageBox.Show("Gia tốc trọng trường nằm trong khoảng 5-15m/s²");
                        }
                        else
                        {
                            ThrowObliqueSettings.Angle = float.Parse(_alpha.Text);
                            ThrowObliqueSettings.Velocity = float.Parse(_v0.Text);
                            ThrowObliqueSettings.Gravity = float.Parse(_g.Text);
                            ThrowObliqueSettings.HeightFirst = float.Parse(_h_fist.Text);

                            ThrowObliqueSettings.check_apply = true;

                            NavigationService.GoBack();
                        }
                    }
                }
            }

            
        }

        private void v0_slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            float value = 5 + 45 * (float)v0_slider.Value/10;
            _v0.Text = value.ToString();
        }

        private void alpha_slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            float value = 90 * (float)alpha_slider.Value/10;
            _alpha.Text = value.ToString();
        }

        private void h_slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            float value = 50 * (float)h_slider.Value/10;
            _h_fist.Text = value.ToString();
        }

        private void g_slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            float value = 5 + (float)g_slider.Value;
            _g.Text = value.ToString();
        }

        private void _v0_LostFocus(object sender, RoutedEventArgs e)
        {
            v0_slider.Value = (float.Parse(_v0.Text) - 1) / 49 * 10;
            
        }

        private void _alpha_LostFocus(object sender, RoutedEventArgs e)
        {
            alpha_slider.Value = (float.Parse(_alpha.Text)) / 90 * 10;
            
        }

        private void _h_fist_LostFocus(object sender, RoutedEventArgs e)
        {
            h_slider.Value = (float.Parse(_h_fist.Text)) / 50 * 10;
            
        }

        private void _g_LostFocus(object sender, RoutedEventArgs e)
        {
            g_slider.Value = (float.Parse(_g.Text) - 5);
        }
    }
}